package com.egene.tw.activity.contact;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.easemob.easeui.EaseConstant;
import com.easemob.easeui.ui.EaseChatFragment;
import com.egene.tw.R;
import com.egene.tw.dao.UserDao;
import com.egene.tw.model.User;
import com.egene.tw.utils.NetworkUtil;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;

@EFragment
public class ContactOnlineFragment extends Fragment {
    private View rootView;//缓存Fragment view

    @Bean
    UserDao userDao;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(rootView==null){
            User user = userDao.getUser();
            user.getUserType();

            rootView=inflater.inflate(R.layout.fragment_contact_online, null);

            if("2".equals(user.getUserType())){

                EaseChatFragment fragment = new EaseChatFragment();
                fragment.setArguments(getBundle());
                getChildFragmentManager().beginTransaction().add(R.id.root, fragment).commit();
            }


//            wvChat = (WebView) rootView.findViewById(R.id.wvChat);
//            wvChat.loadUrl(ApiConstant.BASE_URL + "/admin/message/online_client.aspx?userid=" + user.getUserID());
////            System.out.println(ApiConstant.BASE_URL + "/admin/message/online_client.aspx?userid=" + user.getUserID());
//            wvChat.getSettings().setJavaScriptEnabled(true);
//            wvChat.setWebViewClient(new WebViewClient(){
//                @Override
//                public void onPageFinished(WebView view, String url) {
//                    super.onPageFinished(view, url);
//                    if(NetworkUtil.isNetworkConnected()){
//                        wvChat.setVisibility(View.VISIBLE);
//                    }else{
//                        rootView = null;
//                        wvChat.setVisibility(View.GONE);
//                    }
//                }
//            });
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }




    private Bundle getBundle() {
        Bundle bundle = new Bundle();
        bundle.putString(EaseConstant.EXTRA_SELF_USER_ID,userDao.getUser().getUserID());
        return bundle;
    }

}
